<template>
    <div class="app-container">
        <el-page-header @back="goBack" >
        </el-page-header>
      <div>
        <el-table
          v-loading="loading"
          border
          height="60vh"
          :data="dataList"
          show-summary
          :summary-method="getSummaries"
        >
          <el-table-column label="序号" width="60" type="index" align="center" />
          <el-table-column
            label="发货单编码"
            min-width="200"
            align="center"
            prop="code"
            v-if="columns[1].visible"
          >
            <template v-slot="{ row }">
              <el-button type="primary" @click="handleView(row)" text>{{
                row.code
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="生产单编码"
            min-width="200"
            align="center"
            prop="produceNo"
            v-if="columns[1].visible"
          >
          </el-table-column>
  
          <el-table-column
            label="书刊编码"
            min-width="120"
            align="center"
            prop="bookCode"
            v-if="columns[2].visible"
          >
          </el-table-column>
          <el-table-column
            label="书刊名称"
            min-width="280"
            align="center"
            prop="bookName"
            v-if="columns[3].visible"
          >
          </el-table-column>
          <el-table-column
            label="发货日期"
            min-width="120"
            align="center"
            :show-overflow-tooltip="true"
            prop="deliveryDate"
            v-if="columns[4].visible"
          >
            <template v-slot="{ row }">
              {{ row.deliveryDate?.split("T")[0] ?? "" }}
            </template>
          </el-table-column>
          <el-table-column
            label="客户名称"
            min-width="120"
            align="center"
            prop="customName"
            v-if="columns[5].visible"
          >
          </el-table-column>
          <el-table-column
            label="发货数量"
            min-width="120"
            align="center"
            prop="quantity"
            v-if="columns[5].visible"
          >
          </el-table-column>
          <el-table-column
            label="订单数量"
            min-width="120"
            align="center"
            prop="orderCount"
            v-if="columns[5].visible"
          >
          </el-table-column>
          <!-- <el-table-column
            label="印张 "
            min-width="120"
            align="center"
            prop="sheetCount"
            v-if="columns[5].visible"
          >
          </el-table-column> -->
          <el-table-column
            label="纸令数"
            min-width="120"
            align="center"
            prop="reamAmount"
            v-if="columns[5].visible"
          >
          </el-table-column>
          <el-table-column
            label="创建人"
            min-width="120"
            align="center"
            :show-overflow-tooltip="true"
            prop="createBy"
            v-if="columns[6].visible"
          >
          </el-table-column>
          <el-table-column
            label="创建日期"
            min-width="120"
            align="center"
            :show-overflow-tooltip="true"
            prop="createTime"
            v-if="columns[7].visible"
          >
            <template v-slot="{ row }">
              {{ row.createTime?.split("T")[0] ?? "" }}
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            min-width="120"
            align="center"
            :show-overflow-tooltip="true"
            prop="remark"
            v-if="columns[8].visible"
          >
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </template>
  
  <script setup>
  // 编号改成序列号 纸张编号 印刷编号
  import { downLoadBlob } from "@/utils/ruoyi";
  import { getDeliveryDetail} from "@/api/production/invoice.js";
  import useTagsViewStore from "@/store/modules/tagsView";
  import { getToken } from "@/utils/auth";
  const tagsViewStore = useTagsViewStore();
  const { proxy } = getCurrentInstance();
  const loading = ref(true);
  const dataList = ref([]);
  const showSearch = ref(true);
  const total = ref(0);
  const router = useRouter();
  const route = useRoute();
  const mainId = ref("");
  const printOpen = ref(false);
  const columns = ref([
    { key: 0, label: `状态`, visible: true },
    { key: 1, label: `发货单编码`, visible: true },
    { key: 2, label: `书刊编码`, visible: true },
    { key: 3, label: `书刊名称`, visible: true },
    { key: 4, label: `发货日期`, visible: true },
    { key: 5, label: `客户名称`, visible: true },
    { key: 6, label: `创建人`, visible: true },
    { key: 7, label: `创建时间`, visible: true },
    { key: 8, label: `备注`, visible: true },
  ]);
  
  const getSummaries = (param) => {
    const { columns, data } = param;
    const sums = [];
    const fieldsToSum = ["status", "remark"];
  
    columns.forEach((column, index) => {
      if (index === 0) {
        sums[index] = "合计";
        return;
}
      if (!fieldsToSum.includes(column.property)) {
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index];
        } else {
          sums[index] = "";
        }
      }
    });
  
    return sums;
  };

  const data = reactive({
    queryParams: {
      pageNum: 1,
      pageSize: 50,
      produceNo:undefined,
    },
  });
  
  const { queryParams } = toRefs(data);
  //这里是打印的配置项
  const { ems_order_status } = proxy.useDict("ems_order_status");
  
  const getList = () => {
    loading.value = true;
    if(route.query?.produceNo){
      queryParams.value.produceNo=route.query.produceNo;
    }
    
    getDeliveryDetail(queryParams.value)
      .then((res) => {
        loading.value = false;
        dataList.value = res.rows;
        total.value = res.total;
      })
      .catch((err) => {
        loading.value = false;
      });
  };

  // 查看
const handleView = (row) => {
  router.push({
    path: "/production/invoice/invoiceDetails",
    query: {
      id: row.id,
      info: true,
    },
  });
};
  
  // 导出
  const handleExport = () => {
    deliveryExport(queryParams.value).then((res) => {
      downLoadBlob(res, "发货单列表");
    });
  };
  
  getList();

    //  返回
    const goBack = () => {
    setTimeout(() => {
    router.go(-1);
    tagsViewStore.delView(route);
  }, 0);
  };

  </script>
  
  <style lang="scss" scoped>
  .operation {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 18px;
  }
  
  </style>
  